﻿using Common;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Web.Hanlder
{
    public class Mes_typeHandler : IHttpHandler
    {
        #region Base
        HttpRequest Request;
        HttpResponse Response;
        System.Web.SessionState.HttpSessionState Session;
        HttpServerUtility Server;
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            Request = context.Request;
            Response = context.Response;
            Session = context.Session;
            Server = context.Server;
            string method = Request["Method"].ToString();
            System.Reflection.MethodInfo methodInfo = this.GetType().GetMethod(method);
            methodInfo.Invoke(this, null);
        }
        public List<Model.tb_mes_type> GetOBJList()
        {
            return Common.SqlExption.GetList<Model.tb_mes_type>();
        }
        public void GetList()
        {
            var data = Common.SqlExption.GetList<Model.tb_mes_type>().AsQueryable();
            if (Request["name"] != null)
            {
                string txt_search = Request["name"];
                data = data.Where(c => c.name.Contains(txt_search));
            }
            string result = JsonConvert.SerializeObject(data.ToList());
            Response.Write(result);
            Response.End();

        }
        public void Del()
        {
            ReturnHelper<DBNull> rh = new ReturnHelper<DBNull>();
            int Id = int.Parse(Request["Id"]);

            var model = GetOBJList().FirstOrDefault(c => c.Id.Equals(Id));
            if (model != null)
            {
                Common.SqlExption.Delete<Model.tb_mes_type>(Id);
            }
            rh.states = 0;
            string jsonSucess = JsonConvert.SerializeObject(rh);

            Response.Write(jsonSucess);
            Response.End();
        }
        public void Edit()
        {


            ReturnHelper<DBNull> rh = new ReturnHelper<DBNull>();
 int Id = Convert.ToInt32( Request["Id"]);
 if ( !int.TryParse(Request["Id"],out Id) )
{
 if (Id<=0)
{
rh.states = 1;
rh.msg = " 验证错误！";
string json = JsonConvert.SerializeObject(rh);
 Response.Write(json);
Response.End();
}
}
 var name = Request["name"];
if (name.Trim().Length <= 0)
 {
rh.states = 1;
rh.msg = " 验证错误！";
string json = JsonConvert.SerializeObject(rh);
 Response.Write(json);
Response.End();
}
            var model = Common.SqlExption.GetList<Model.tb_mes_type>().FirstOrDefault(c => c.Id.Equals(Id));
            if (model == null)
            {
                model = new Model.tb_mes_type();
model.Id=Id;
model.name=name;
                Common.SqlExption.Add(model);
            }
            else
            {
model.Id=Id;
model.name=name;
                Common.SqlExption.Update(model);
            }
            rh.states = 0;
            string jsonSucess = JsonConvert.SerializeObject(rh);
           
            Response.Write(jsonSucess);
            Response.End();
         
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
        #endregion
    }
}
